<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@include file="../WEB-INF/jspf/controlesessao.jspf"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Lista Componentes</title>
        <script type="text/javascript" src="../js/jquery.js" ></script>
        <script type="text/javascript" src="../js/jquery.validate.js"></script>
        <script type="text/javascript" src="../js/jquery.tablescroll.js"></script>
        <script type="text/javascript" src="../js/forms.js"></script>
        <script type="text/javascript" src="../js/jquery-ui-1.8.2.custom.min.js" ></script>
        <link rel="stylesheet" href="../css/ui-lightness/jquery-ui-1.8.2.custom.css">
    <link rel="stylesheet" href="../css/forms.css"></head>

    <script>
        $(document).ready(function(){
            $("#form_componentes").validate({
                rules: {
                    id_componente: {required: true,digits: true}
                },
                submitHandler: function(form) {
                    $("#msg").dialog({
                        buttons: {
                            Ok: function() {
                                $(this).dialog('close');
                                $("#form").submit();
                            }
                        }
                    });
                    salvar(form);
                }
            });
            
            $("#fm_delete").validate({
                rules: {
                    id_componente: {required: true,digits: true}
                },
                submitHandler: function(form) {
                    $("#msg").dialog({
                        buttons: {
                            Ok: function() {
                                $(this).dialog('close');
                                $("#form").submit();
                            }
                        }
                    });
                    salvar(form);
                }
            });
            $("#todos").click(function(){
                $("input").each(function(){
                    if($(this).is("#componente")){
                        $(this).attr("checked", $("#todos").attr("checked"));
                    }

                });
            })
        });
        function del(id){
            $("#msg").dialog({
                buttons: {
                    Ok: function() {
                        $(this).dialog('close');
                        $("#form").submit();
                    }
                }
            });
            $("#id_componente_delete").val(id.value);
            $("#fm_delete").submit();
        }


    </script>
    <body>
         <form id="fm_delete"  method="post" action="delete_componente.jsp">
             <input type="hidden" name="matricula" value="${param.matricula}">
             <input type="hidden" id="id_componente_delete" name="id_componente" value="">
        <input type="hidden" id="token" name="token" value="${sessionScope.token}" /> </form>
        <form name="form" id="form" method="post" >
            <p>
                <label class="lbInput">Descrição</label>
                <input name="desc" value="${param.desc}">
            </p>
            <input type="image" class="noborder" src="../imagens/procurar.png" onclick="submit()">
        <input type="hidden" id="token" name="token" value="${sessionScope.token}" /> </form>
        <br>
        <c:if test="${!empty param.desc}">
            <sql:query var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
                SELECT  Componentes.*
                FROM Componentes
                where desc_componente like ?
                and not exists(select 1 from Componentes_Docentes cd
                where cd.id_componente=Componentes.id_componente
                and cd.matricula=?)
                <sql:param value="${param.desc}%"/>
                <sql:param value="${param.matricula}"/>
                order by desc_componente
            </sql:query>
            <form id="form_componentes" method="post" action="insert_componentes.jsp">
                <input type="hidden" name="matricula" value="${param.matricula}">
                <input type="submit" class="submit" name="gravar" value="Adicionar Componentes">
                <table cellspacing="0" width="90%" align="center">
                    <thead>
                        <th><input type="checkbox" name="todos" id="todos"></th>
                        <th>Código</th>
                        <th>Componente</th>
                        <th>Carga Horária</th>
                    </thead>
                    <tfoot>
                        <tr>
                            <td colspan="4">
                                ${query.rowCount} registros encontrados
                            </td>
                        </tr>
                    </tfoot>
                    <c:forEach var="row" items="${query.rows}">
                        <tr>
                            <td><input name="componente" id="componente" type="checkbox" value="${row.id_componente}"></td>
                            <td>${row.codigo_componente}</td>
                            <td>${row.desc_componente}</td>
                            <td>${row.carga_componente}</td>
                        </tr></c:forEach>
                    </table>
                <input type="hidden" id="token" name="token" value="${sessionScope.token}" /> </form>
        </c:if>
        <c:if test="${!empty param.matricula}">

            <div id="componentes_docente">
                <p>
                    Componentes do docente
                </p>
                    <sql:query var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
                        SELECT  Componentes.*
                        FROM Componentes
                        where exists(select 1 from Componentes_Docentes cd
                        where cd.id_componente=Componentes.id_componente
                        and cd.matricula=?)
                        <sql:param value="${param.matricula}"/>
                        order by desc_componente
                    </sql:query>

                    <table cellspacing="0" width="90%" align="center">
                        <thead>
                            
                            <th>Código</th>
                            <th>Componente</th>
                            <th>Carga Horária</th>
                            <th></th>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="4">
                                    ${query.rowCount} registros encontrados
                                </td>
                            </tr>
                        </tfoot>
                        <c:forEach var="row" items="${query.rows}">
                            <tr>
                                
                                <td>${row.codigo_componente}</td>
                                <td>${row.desc_componente}</td>
                                <td>${row.carga_componente}</td>
                                <td>                                    
                                    <input type="image" class="noborder" src="../imagens/remover.png" value="${row.id_componente}" onclick="del(this)">
                               </td>
                            </tr></c:forEach>
                        </table>
                </div>
        </c:if>
        <div id="msg" style="display:none"></div>

    </body>
</html>